package com.xqq.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xqq.entity.ConflictEvent;

import java.util.ArrayList;
import java.util.List;

public class ConflictCsvParser {
    public static void main(String[] args) {
        String fileName = "Asia-Pacific_2018-2025_May16";
        String filePath = "F:\\XW\\项目相关\\缅甸\\冲突数据\\";
        String startYear = "2024";
        String endYear = "2025";
        String country = "Myanmar";
        String inputCsvFile = filePath + fileName + ".csv"; // 输入的 CSV 文件路径
        String outputCsvFile = filePath + fileName + "_" + startYear + "_" + endYear +"_"+ country + ".csv"; // 输出的过滤后 CSV 文件路径
        List<ConflictEvent> filteredRows = new ArrayList<>();
        // 读取 CSV 文件
        EasyExcel.read(inputCsvFile, ConflictEvent.class, new AnalysisEventListener<ConflictEvent>() {
            @Override
            public void invoke(ConflictEvent dataRow, AnalysisContext context) {
                // 根据条件进行过滤
                if (dataRow.getYear() != null && (startYear.equals(dataRow.getYear()) || endYear.equals(dataRow.getYear())) && country.equals(dataRow.getCountry())) {
                    filteredRows.add(dataRow);
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 读取完成后的处理
            }
        }).sheet().doRead();
        // 写入过滤后的数据
        EasyExcel.write(outputCsvFile, ConflictEvent.class).sheet("Filtered Data").doWrite(filteredRows);
        System.out.println("过滤后的 CSV 文件已成功保存到: " + outputCsvFile);
        //TODO 后续可以执行 readCsvAndSaveToDatabase(outputCsvFile)
    }

}
